/**
 * File:   NodesTab.java
 * Author: Gvennet
 */
package gui.tabs;

import dht.network.Network;
import dht.node.DHTNodeEventListener;
import dht.node.DHTNodeModel;
import dht.node.DHTNodeRow;
import gui.actions.AddNodeAction;
import gui.actions.DeleteNodeAction;
import gui.actions.GetNodesAction;
import gui.actions.PingNodeAction;
import gui.main.DHTNodeTableModel;

import javax.swing.*;
import java.awt.*;

/**
 * todo write javadoc
 */
public final class NodesTab
{

	public static JComponent createNodesTab(JFrame frame, Network network, DHTNodeModel nodeModel)
	{
		JComponent component = new JComponent()
		{

		};

		component.setLayout(new BorderLayout());

		final DHTNodeTableModel nodeTableModel = new DHTNodeTableModel();

		nodeModel.addListener(new DHTNodeEventListener()
		{

			@Override
			public void rowAdded(final DHTNodeRow nodeRow)
			{
				SwingUtilities.invokeLater(new Runnable()
				{

					@Override
					public void run()
					{
						nodeTableModel.addRow(nodeRow);
					}
				});
			}

			@Override
			public void rowRemoved(final DHTNodeRow nodeRow)
			{
				SwingUtilities.invokeLater(new Runnable()
				{

					@Override
					public void run()
					{
						nodeTableModel.removeRow(nodeRow);
					}
				});
			}

			@Override
			public void rowUpdated(final DHTNodeRow nodeRow)
			{
				SwingUtilities.invokeLater(new Runnable()
				{

					@Override
					public void run()
					{
						nodeTableModel.updateRow(nodeRow);
					}
				});
			}

		});

		JTable nodeTable = new JTable(nodeTableModel);
		nodeTable.getColumnModel().getColumn(3).setCellRenderer(new DHTNodeTableModel.DateRenderer());


		JButton addNodeButton = new JButton(new AddNodeAction(frame, network));
		JButton deleteNodeButton = new JButton(new DeleteNodeAction(nodeModel, nodeTable));
		JButton pingNodeButton = new JButton(new PingNodeAction(nodeModel, nodeTable, network));
		JButton getNodesButton = new JButton(new GetNodesAction(nodeModel, nodeTable, network));

		JPanel controlPanel = new JPanel();

		GroupLayout layout = new GroupLayout(controlPanel);
		controlPanel.setLayout(layout);

		layout.setAutoCreateGaps(true);
		layout.setAutoCreateContainerGaps(true);

		GroupLayout.SequentialGroup hGroup = layout.createSequentialGroup();

		hGroup.addGroup(layout.createParallelGroup()
				.addGroup(layout.createSequentialGroup().addComponent(addNodeButton).addComponent(deleteNodeButton)
				.addComponent(pingNodeButton).addComponent(getNodesButton)));
		layout.setHorizontalGroup(hGroup);

		GroupLayout.SequentialGroup vGroup = layout.createSequentialGroup();

		vGroup.addGroup(layout.createParallelGroup(GroupLayout.Alignment.BASELINE)
				.addComponent(addNodeButton).addComponent(deleteNodeButton)
				.addComponent(pingNodeButton).addComponent(getNodesButton));
		layout.setVerticalGroup(vGroup);
		layout.linkSize(pingNodeButton, getNodesButton);


		FlowLayout flowLayout = new FlowLayout();
		flowLayout.setAlignment(FlowLayout.LEFT);

//        JPanel controlPanel = new JPanel(flowLayout);
//        controlPanel.add(new JButton(new AddNodeAction(frame, network)));
//        controlPanel.add(new JButton(new DeleteNodeAction(nodeModel, nodeTable)));
//        controlPanel.add(new JButton(new PingNodeAction(nodeModel, nodeTable, network)));
//        controlPanel.add(new JButton(new GetNodesAction(nodeModel, nodeTable, network)));

		component.add(BorderLayout.SOUTH, controlPanel);
		component.add(BorderLayout.CENTER, new JScrollPane(nodeTable));

		component.setVisible(true);

		return component;

	}

}
